package org.kkhweb.checkbook;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class CreateNewActivity extends Activity {

	protected SimpleJSONParser jsonParser;
	protected SimpleHTTPClient httpClient;
	protected ProgressDialog dialog;
	protected Button submitButton;	

	protected SharedPreferences settings;
	protected Editor editor;
	protected EditText initField;
	protected EditText nameField;
	protected String name;
	protected String init;
	protected boolean error;
	protected CheckBox prefBox;
	protected boolean pref;
	protected String initText;
	protected float initVal;
	
    public static final String PREFS_NAME = "MyCheckbookSettings";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.createnew);
        this.restore_settings();	    
	    
        submitButton = (Button) findViewById(R.id.fb_createnew_submit);
        submitButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
        		nameField = (EditText) findViewById(R.id.ff_createnew_name);
            	name = nameField.getText().toString();
            	initField = (EditText) findViewById(R.id.ff_createnew_init);
            	initText = initField.getText().toString();
            	prefBox = (CheckBox) findViewById(R.id.fbox_createnew_preferred);
            	pref = prefBox.isChecked();
            	
        		try {
        			initVal = Float.parseFloat(initText);
        			error = false;
        		} catch(NumberFormatException ex) {
        			toast("Initial Balance Must Be a Number");
        			initField.setText("");
        			error = true;
        		}
        		
        		if(!error) {
        			createNewThread();
        		}
            }
        });
	}//End onCreate

	@Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
    	super.onStop();
        // The activity is no longer visible (it is now "stopped")
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
    }

//SignIn Thread    
	protected void createNewThread()
	{
		httpClient = new SimpleHTTPClient();
		
    	dialog = ProgressDialog.show(CreateNewActivity.this, "Creating New Checkbook", 
                "Please wait...", false);  		    

	    new Thread() {
		    @Override
		    public void run()
		    {         
        		httpClient.addFormData("user_id", Integer.toString(settings.getInt("user_id", 0)));
		    	httpClient.addFormData("name", name);
		    	httpClient.addFormData("init_balance", Float.toString(initVal));
		    	httpClient.addFormData("preferred", Boolean.toString(pref));
		    	httpClient.setUserAgent("MyCheckbook For Android");
		    	httpClient.setURL(Globals.buildURL("create_new"));
		    	httpClient.sendRequest();
		        
		        handler.sendEmptyMessage(0);
		    }
	    }.start();
	}

//Handler for SignInThread    
	protected Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			dialog.dismiss();
    		
    		if(httpClient.hasError()) {
    			toast(httpClient.getError());
    		} else {
    			toast(httpClient.getResponse());
    			finish();
    		}
		}
	};	

	//Toast Messages    
    protected void toast(String msg)
    {
    	Context context = getApplicationContext();
    	Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();    	
    }  	
	
	protected void restore_settings()
	{
        settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
        editor = settings.edit();        
	}	
	
}//End SignInActivity